//
//  ForgetPwdController.m
//  EyeCare
//
//  Created by 韩盼盼 on 2016/12/22.
//  Copyright © 2016年 HG. All rights reserved.
//

#import "ForgetPwdController.h"
#import "ResetPwdViewController.h"

@interface ForgetPwdController ()<UITextFieldDelegate>
{
    UITextField *textField_tel;
    UITextField *textField_captch;
    UIView *contentViews;
    
    int starTime;
    UIButton *nextBtn;

    
    UIButton *getCaptchBtn;
}

@property (nonatomic,strong) NSMutableArray *mutable_checks;

@end

@implementation ForgetPwdController
-(NSMutableArray *)mutable_checks{
    if (!_mutable_checks) {
        _mutable_checks=[[NSMutableArray alloc]initWithObjects:@"0",@"0", nil];
    }
    return _mutable_checks;
}

- (void)viewDidLoad {
//    self.navBarType = NO;
    [super viewDidLoad];
    self.view.backgroundColor = HGCOLOR(@"f6f6f6");
    [self updateNav];
    [self initWithSubViews];
}


#pragma mark - draw views

-(void)updateNav
{
//    [self.navigateBar.leftBtn setHidden:NO];
//    
//    [self.navigateBar.bgImageView setBackgroundColor:[UIColor whiteColor]];
//    [self.navigateBar.backImg setImage:[UIImage imageNamed:@"left_arrow_blue"]];
    
    [self setTitle: @"忘记密码"];
//    self.navigateBar.titleLabel.textColor = RGBCOLOR(51, 51, 51);
    
//    self.navigateBar.titleLabel.whc_CenterX(0).whc_Height(44).whc_TopSpace(20);
//    [self.navigateBar.lineImageView setHidden:NO];
}

//-(void)updateNav
//{
//    [self.navigateBar.bgImageView setBackgroundColor:HGCOLOR(@"ffffff")];
//    [self.navigateBar.lineImageView setHidden:YES];
//    self.navigateBar.titleLabel.textColor = HGCOLOR(@"b00000");
//    self.navigateBar.titleLabel.text = @"忘记密码";
//    [self.navigateBar.leftBtn setImage:[UIImage imageNamed:@"left_b_back"] forState:UIControlStateNormal];
//}

-(void)initWithSubViews{
    contentViews =[[UIView alloc]initWithFrame:CGRectMake(0, 64, __kScreenWidth , __kScreenHeight - 64)];
    contentViews.backgroundColor = HGCOLOR(@"ffffff");
    [self.view addSubview:contentViews];
    
    
    UILabel *alertlab = [[UILabel alloc]initWithFrame:CGRectMake(HGADPT(48), HGADPT(57), __kScreenWidth - 2*HGADPT(48), HGADPT(25))];
    alertlab.text = @"请确认您的手机畅通，以便接收注册验证码短信";
    alertlab.textColor = HGCOLOR(@"fe7200");
    alertlab.font=HGFont(24);
    [contentViews addSubview:alertlab];
    
    textField_tel = [[UITextField alloc]initWithFrame:CGRectMake(MARGIN_LEFT, alertlab.bottom+HGADPT(21), __kScreenWidth - 2*MARGIN_LEFT , HGADPT(98))];
    textField_tel.backgroundColor = HGCOLOR(@"ffffff");
    textField_tel.layer.borderColor = HGCOLOR(@"e1e1e1").CGColor;
    textField_tel.layer.borderWidth = HGADPT(1);
    textField_tel.clipsToBounds = YES;
    textField_tel.placeholder = @"请输入手机号码";
    textField_tel.leftView = [self getLeftView:@"login_usertel"];
    textField_tel.leftViewMode = UITextFieldViewModeAlways;
    [textField_tel setValue:@"11" forKey:@"LimitInput"];
    textField_tel.layer.cornerRadius = 10 * ADPT;
    textField_tel.tintColor = app_base_color;
    textField_tel.rightView = [self getRightView];
    textField_tel.rightViewMode =UITextFieldViewModeAlways;

    textField_tel.font = HGFont(30);
    textField_tel.delegate = self;
    [textField_tel addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    textField_tel.keyboardType = UIKeyboardTypeNumberPad;
    [contentViews addSubview:textField_tel];
    
    textField_captch = [[UITextField alloc]initWithFrame:CGRectMake(MARGIN_LEFT, textField_tel.bottom+HGADPT(30), __kScreenWidth - 2*MARGIN_LEFT , HGADPT(98))];
    textField_captch.placeholder =@"请输入6位短信验证码,15分钟内有效";
    textField_captch.tintColor = app_base_color;
    textField_captch.backgroundColor = HGCOLOR(@"ffffff");
    textField_captch.layer.borderColor = HGCOLOR(@"e1e1e1").CGColor;
    textField_captch.layer.borderWidth = HGADPT(1);
    textField_captch.clipsToBounds = YES;
    textField_captch.leftView = [self getLeftView:@"login_captch"];
    textField_captch.leftViewMode = UITextFieldViewModeAlways;
    [textField_captch setValue:@"6" forKey:@"LimitInput"];
    textField_captch.layer.cornerRadius = 10 * ADPT;
    textField_captch.font = HGFont(30);
    textField_captch.delegate = self;
    [textField_captch addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    textField_captch.keyboardType = UIKeyboardTypeNumberPad;
    [contentViews addSubview:textField_captch];
    
    nextBtn =[[UIButton alloc]initWithFrame:CGRectMake(MARGIN_LEFT, textField_captch.bottom+HGADPT(50), __kScreenWidth - 2*MARGIN_LEFT, HGADPT(98))];
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextBtn setTitleColor:HGCOLOR(@"ffffff") forState:UIControlStateNormal];
    nextBtn.layer.cornerRadius = 10 * ADPT;
    nextBtn.titleLabel.font = HTFont(36 * ADPT);
    nextBtn.backgroundColor = kRGBCOLOR(221, 221, 221);
    nextBtn.userInteractionEnabled = NO;
    [nextBtn addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentViews addSubview:nextBtn];
}

-(void)nextAction:(UIButton *)btn
{
    [self HG_CheckCaptch];
}

-(UIView *)getLeftView:(NSString *)imgstr{
    UIView *leftView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, HGADPT(88), HGADPT(98))];
    UIImage *image=[UIImage imageNamed:imgstr];
    
    UIImageView *lefticon=[[UIImageView alloc]initWithFrame:CGRectMake(HGADPT(43),(leftView.height- HGADPT(image.size.height))/2 , HGADPT(image.size.width), HGADPT(image.size.height))];
    [leftView addSubview:lefticon];
    lefticon.image =image;
    return leftView;
}

-(UIView *)getRightView
{
    UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, HGADPT(190), HGADPT(90))];
    getCaptchBtn = [[UIButton alloc]initWithFrame:CGRectMake(HGADPT(1), (rightView.height - HGADPT(50))/2, HGADPT(150), HGADPT(50))];
    [getCaptchBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    getCaptchBtn.titleLabel.font = HGFont(24);
    [rightView addSubview:getCaptchBtn];
    [getCaptchBtn addTarget:self action:@selector(HG_GetCaptch) forControlEvents:UIControlEventTouchUpInside];
    getCaptchBtn.layer.borderWidth = HGADPT(1);

    [getCaptchBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [getCaptchBtn setBackgroundColor:app_base_color];
    getCaptchBtn.layer.borderColor = app_base_color.CGColor;
    [BaseViewController getRoundIcon:getCaptchBtn borderWidth:0 borderColor:app_base_color cornerRadius:8 * ADPT];
//    getCaptchBtn.userInteractionEnabled = NO;
    
    return rightView;
}

#pragma mark -- 时间倒计时
- (void)startTime
{
    starTime=60;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(starTime<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [getCaptchBtn setTitle:@"重新发送" forState:UIControlStateNormal];
                getCaptchBtn.userInteractionEnabled = YES;
//                [getCaptchBtn setTitleColor:HGCOLOR(@"ff4865") forState:UIControlStateNormal];
                getCaptchBtn.layer.borderColor = HGCOLOR(@"ff4865").CGColor;
            });
        }else{
            
            NSString *strTime = [NSString stringWithFormat:@"%.2d", starTime];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [getCaptchBtn setTitle:[NSString stringWithFormat:@"(%@)s发送",strTime] forState:UIControlStateNormal];
//                [getCaptchBtn setTitleColor:HGColor(221, 221, 221) forState:UIControlStateNormal];
                getCaptchBtn.layer.borderColor = kRGBCOLOR(221, 221, 221).CGColor;
                
                getCaptchBtn.titleLabel.numberOfLines=2;
                getCaptchBtn.userInteractionEnabled = NO;
                
            });
            starTime--;
        }
    });
    dispatch_resume(_timer);
}


-(void)textFieldEditingChanged:(UITextField *)textField
{
    if (textField_tel == textField) {
        if (textField_tel.text.length>10) {
            if ([HGDataCheck isMobileNumber_str:textField_tel.text]) {
                getCaptchBtn.userInteractionEnabled = YES;
//                [getCaptchBtn setTitleColor:HGCOLOR(@"ff4865") forState:UIControlStateNormal];
                getCaptchBtn.layer.borderColor = HGCOLOR(@"ff4865").CGColor;
                [self checkMobile];
                [self.mutable_checks replaceObjectAtIndex:0 withObject:@"1"];
                
            }
            else{
                [MBProgressHUD showSuccess:@"请输入正确的手机号！"];
//                [getCaptchBtn setTitleColor:HGColor(221, 221, 221) forState:UIControlStateNormal];
                getCaptchBtn.layer.borderColor = HGColor(221, 221, 221).CGColor;
                getCaptchBtn.userInteractionEnabled = NO;
                [self.mutable_checks replaceObjectAtIndex:0 withObject:@"0"];
                
                
            }
        }
        else
        {
            nextBtn.backgroundColor = HGColor(221, 221, 221);
            nextBtn.userInteractionEnabled = NO;
            
        }
    }
    if (textField_captch ==textField) {
        if (textField.text.length>5) {
            [self.mutable_checks replaceObjectAtIndex:1 withObject:@"1"];
//            [self HG_CheckCaptch];
        }
        else
        {
            [self.mutable_checks replaceObjectAtIndex:1 withObject:@"0"];
            nextBtn.backgroundColor = HGColor(221, 221, 221);
            nextBtn.userInteractionEnabled = NO;
        }
    }
    [self checkInputContent];
    
}
-(void)checkInputContent
{
    if ([self.mutable_checks containsObject:@"0"]) {
        nextBtn.backgroundColor = HGColor(221, 221, 221);
        nextBtn.userInteractionEnabled = NO;
        
    }
    else
    {
        nextBtn.backgroundColor = app_base_color;
        nextBtn.userInteractionEnabled = YES;
    }
}



-(void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"编辑结束！");
    if (textField_tel == textField) {
        if (textField.text.length<10) {
//            [getCaptchBtn setTitleColor:HGColor(221, 221, 221) forState:UIControlStateNormal];
            getCaptchBtn.layer.borderColor = HGColor(221, 221, 221).CGColor;
            getCaptchBtn.userInteractionEnabled = NO;
            [self.mutable_checks replaceObjectAtIndex:0 withObject:@0];
            [MBProgressHUD showSuccess:textField.text.length==0?@"手机号不能为空！":@"请输入正确的手机号！"];
            
        }
        
    }
    if (textField_captch == textField) {
        if (textField.text.length<5) {
            [MBProgressHUD showSuccess:@"验证码位数不够！"];
        }
    }
}


#pragma mark - 检测手机号！
/**验证手机号*/
-(void)checkMobile
{
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    [params setObject:textField_tel.text forKey:@"mobile"];
//    
//    WS(ws);
//    NSString *url = API_LHF_BASE_URL(MEMBER_MOBILE_CHECK);
//    [HttpRequest requestWhtiURLString:url type:HttpRequestTypeGet parameters:params success:^(id obj) {
//        BOOL isexist = [[obj objectForKey:@"isexist"] boolValue];
//        if (!isexist) {
//            [ws.view makeToast:@"该手机尚未注册!"];
//        }
//    } failure:^(NSError *error) {
//        [ws.view makeToast:error.description];
//    } waitView:ws.view afterDely:0 vc:ws];
    
}

#pragma mark - 获取验证码

-(void)HG_GetCaptch
{
    /*
     mobile	√	string	手机号
     type	√	int	短信类型 可选值:1(注册),2(找回密码),5(绑定第三方登陆账户),7(更换绑定手机(发送验证码给新手机))
     */

//    WS(ws);
//    
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    [params setObject:textField_tel.text forKey:@"mobile"];
//    [params setObject:@2 forKey:@"type"];
//    
//    NSString *url = API_LHF_BASE_URL(CAPTCHA_SEND_MOBILE);
//    [HttpRequest requestWhtiURLString:url type:HttpRequestTypePost parameters:params success:^(id obj) {
//        BOOL issuccessed = [[obj objectForKey:@"issuccess"] boolValue];
//        if (issuccessed) {
//            [ws startTime];
//            [ws.view makeToast:@"验证码已发送到您的手机..."];
//        }else
//        {
//            [ws.view makeToast:@"获取验证码失败..."];
//        }
//    } failure:^(NSError *error) {
//        [ws.view makeToast:error.description];
//    } waitView:ws.view afterDely:0 vc:ws];
    
}


-(void)HG_CheckCaptch
{
    /*
     captcha	√	string	验证码
     mobile	√	string	手机号
     type	√	int	短信类型 可选值:1(注册),2(找回密码),5(绑定第三方登陆账户),7(更换绑定手机(发送验证码给新手机))
     */
    
//    WS(ws);
//
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    [params setObject:textField_captch.text forKey:@"captcha"];
//    [params setObject:textField_tel.text forKey:@"mobile"];
//    [params setObject:@2 forKey:@"type"];
//    
//    NSString *url = API_LHF_BASE_URL(CAPTCHA_CHECK_MOBILE);
//    [HttpRequest requestWhtiURLString:url type:HttpRequestTypeGet parameters:params success:^(id obj) {
//        BOOL istrue = [[obj objectForKey:@"istrue"] boolValue];
//        if (!istrue) {
//            [ws.view makeToast:@"验证码错误"];
//        }else
//        {
//            ResetPwdViewController *setVc=[[ResetPwdViewController alloc]init];
//            setVc.user_tel = textField_tel.text;
//            setVc.user_captch = textField_captch.text;
//            [ws.navigationController pushViewController:setVc animated:YES];
//        }
//    } failure:^(NSError *error) {
//        [ws.view makeToast:error.description];
//    } waitView:ws.view afterDely:0 vc:ws];
    
}



@end
